package com.xyre.client.business.index.view;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.xyre.client.R;
import com.xyre.client.business.index.adapter.GuidesAdapter;
import com.xyre.client.business.index.bean.RegistRequest;
import com.xyre.client.business.index.bean.UserInfo;
import com.xyre.client.business.index.presenter.GuidesPresenter;
import com.xyre.client.business.index.presenter.IGuidesPresenter;

import java.util.List;

import me.relex.circleindicator.CircleIndicator;

/**
 * ==========================================
 * <p/>
 * 版    权 ： 北京爱接力科技有限公司
 * <p/>
 * 作    者 ： iwen
 * <p/>
 * 版    本 ： 1.0
 * <p/>
 * 创建日期 ： on 2016/3/2  14:38
 * <p/>
 * 描    述 ：
 * <p/>
 * 这是新手引导页的fragment碎片，包含：
 * 新手引导的viewpager,cricleIndictor,微信登录入口，手机登录入口
 * 新手引导页中的手机登录会以PhoneLoginFragment的形式展现内容
 * <p/>
 * <p/>
 * 修订历史 ：v1.0 2016.3.02
 * <p/>
 * ==========================================
 */
public class GuidesFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = "ehome";
    // 外层activity容器,可以获取控制器对象
    private IGuidesVIew context;
    // 根视图对象
    private View rootView;
    // viewepager对象
    private ViewPager viewPager;
    // viewpager 的原点指示器
    private CircleIndicator circleIndicator;
    private List<Integer> imageSrcs;
    private GuidesAdapter adapter;
    private TextView text_phone;
    private Button btn_weixinRegist;
    private ProgressDialog progressDialog;


    public GuidesFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = (IGuidesVIew) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.guides, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // 初始化控件
        initView();
        // 模拟获取图片数据,目前使用颜色值来代替图片至
        getData();
        // Ui控件开始加载数据
        initData();

    }

    // 模拟获取数据
    public void getData() {

        IGuidesPresenter presenter = context.getPresenter();
        if (presenter != null) {
            imageSrcs = presenter.getImageResoure();
        }
        presenter = null;
    }

    // 初始化控件
    private void initView() {

        if (rootView == null) {
            return;
        }
        viewPager = (ViewPager) rootView.findViewById(R.id.guides_viewpager);
        circleIndicator = (CircleIndicator) rootView.findViewById(R.id.guides_cricleIndictor);
        text_phone = (TextView) rootView.findViewById(R.id.guides_btn_phone);
        text_phone.setOnClickListener(this);
        btn_weixinRegist = (Button) rootView.findViewById(R.id.guides_btn_weixin);
        btn_weixinRegist.setOnClickListener(this);

    }

    private void initData() {

        if (imageSrcs != null) {
            adapter = new GuidesAdapter(getActivity().getSupportFragmentManager(), getActivity(), imageSrcs);
            viewPager.setAdapter(adapter);
            circleIndicator.setViewPager(viewPager);
            viewPager.setCurrentItem(0);
        }
    }

    /**
     * 关闭对象，释放资源
     */
    @Override
    public void onDestroy() {
        GuidesPresenter presenter = (GuidesPresenter) context.getPresenter();
        presenter.guidesFragment = null;
        presenter = null;
        super.onDestroy();
        context = null;
        rootView = null;
        viewPager = null;
        circleIndicator = null;
        imageSrcs = null;
        adapter = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 点击事件
     * 1.点击切换手机注册按钮事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {

        int id = v.getId();
        switch (id) {

            // 切换手机注册页面fragment,具体实现由控制器实现
            case R.id.guides_btn_phone:

                context.getPresenter().natigiveToPhoneRegistFragment();
                break;

            case R.id.guides_btn_weixin:

                // 显示微信注册登录等待状态
                showLoading();
                // 调用控制器的网络访问
                context.getPresenter().weixinRegist(new RegistRequest("1", "", "", "110", "ww.sina.coiml.cn", "小白"));
                break;

        }


    }

    /**
     * 显示微信注册失败状态
     *
     * @param msg
     */
    public void showStateWeixinRegistError(String msg) {

        // 关闭等待窗体
        closeLoading();
        // 提示信息
        Toast.makeText(getActivity(), "微信注册失败,失败信息: " + msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示微信注册成功状态
     *
     * @param info
     */
    public void showStateWeixinRegistSuccess(UserInfo info) {

        // 关闭等待窗体
        closeLoading();
        // 提示信息
        Toast.makeText(getActivity(), "微信注册成功 ", Toast.LENGTH_SHORT).show();
    }


    // 显示微信注册登录等待状态
    private void showLoading() {

        btn_weixinRegist.setEnabled(false);
        getProgressDialog("微信正在注册，请稍等......").show();
    }

    // 关闭微信注册登录等待状态
    private void closeLoading() {
        btn_weixinRegist.setEnabled(true);
        getProgressDialog("").dismiss();
    }


    /**
     * 获取网络等待显示窗体
     *
     * @param title
     * @return
     */
    private ProgressDialog getProgressDialog(String title) {

        if (progressDialog == null) {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setTitle(title);
        }
        return progressDialog;
    }


}
